package com.campusbackend.config;

import com.campusbackend.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/adminUser/login",
                        "/teacher/login",
                        "/students/login",
                        "/error",
                        "/teacher/register",
                        "/students/register",
                        "/teacher/checkEmployeeId",
                        "/teacher/checkIdCard",
                        "/teacher/checkPhone",
                        "/teacher/checkEmail",
                        "/files/**",
                        "/public/**",
                        "/skills/list",
                        "/teacher/getCompleteInfo"
                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源访问
        String uploadPath = fileUploadConfig.getUpload().getPath();
        registry.addResourceHandler("/files/**")
                .addResourceLocations("file:" + uploadPath + "/");
    }
}
